﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VehicleScale.Models;
using VehicleScale.EntityMappers;
using System.Diagnostics;

namespace VehicleScale.DataAccess
{
    public class VechileScaleDetailDataAccess : BaseDataAccess, IVehicleScaleDetailDataAccess
    {
        /// <summary>
        /// Add Method
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Add(VehicleScaleDetailModel item, string vehicleScaleID, out string detailerror)
        {
            detailerror = String.Empty;
            bool result = false;
            try
            {
                using (DataClassesDataContext dataContext = new DataClassesDataContext(base.ConnectionString))
                {

                    // item.VehicleScaleID = Guid.NewGuid();
                    VehicleScaleDetail entity = new VehicleScaleDetail();
                    VehicleScaleDetailMapper.ToEntity(entity, item);

                    dataContext.VehicleScaleDetails.InsertOnSubmit(entity);
                    dataContext.SubmitChanges();
                    result = true;
                    item.IsNew = false;
                    item.IsEdit = false;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                detailerror = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// Edit Method
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Edit(VehicleScaleDetailModel item, out string detailerror)
        {
            detailerror = String.Empty;
            bool result = false;
            try
            {
                using (DataClassesDataContext dataContext = new DataClassesDataContext(base.ConnectionString))
                {
                    var entity = (from v in dataContext.VehicleScaleDetails
                                  where v.VehicleScaleDetailID == item.VehicleScaleDetailID
                                  select v).FirstOrDefault();
                    if (entity != null)
                    {
                        VehicleScaleDetailMapper.ToEntity(entity, item);

                        dataContext.SubmitChanges();
                        result = true;
                        item.IsNew = false;
                        item.IsEdit = false;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                detailerror = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// Delete Method
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Delete(VehicleScaleDetailModel item)
        {
            bool result = false;
            try
            {
                using (DataClassesDataContext dataContext = new DataClassesDataContext(base.ConnectionString))
                {
                    var entity = (from v in dataContext.VehicleScaleDetails
                                  where v.VehicleScaleDetailID == item.VehicleScaleDetailID
                                  select v).FirstOrDefault();
                    if (entity != null)
                    {
                        dataContext.VehicleScaleDetails.DeleteOnSubmit(entity);
                        dataContext.SubmitChanges();

                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// Add Method
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Save(VehicleScaleDetailModel item, string vehicleScaleID, out string detailerror)
        {
            detailerror = String.Empty;
            bool result = false;
            try
            {
                using (DataClassesDataContext dataContext = new DataClassesDataContext(base.ConnectionString))
                {

                    var entity = (from v in dataContext.VehicleScaleDetails
                                  where v.VehicleScaleDetailID == item.VehicleScaleDetailID
                                  select v).FirstOrDefault();
                    if (entity != null)
                    {
                        VehicleScaleDetailMapper.ToEntity(entity, item);
                    }
                    else
                    {
                        entity = new VehicleScaleDetail();
                        VehicleScaleDetailMapper.ToEntity(entity, item);
                        dataContext.VehicleScaleDetails.InsertOnSubmit(entity);
                    }

                    dataContext.SubmitChanges();
                    result = true;
                    item.IsNew = false;
                    item.IsEdit = false;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                detailerror = ex.Message;
            }
            return result;
        }

        #region IBaseDataAccess<VehicleScaleDetailModel> Members

        VehicleScaleDetailModel IBaseDataAccess<VehicleScaleDetailModel>.SearchObject
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        IList<VehicleScaleDetailModel> IBaseDataAccess<VehicleScaleDetailModel>.Searcher()
        {
            throw new NotImplementedException();
        }

        VehicleScaleDetailModel IBaseDataAccess<VehicleScaleDetailModel>.Get()
        {
            throw new NotImplementedException();
        }

        VehicleScaleDetailModel IBaseDataAccess<VehicleScaleDetailModel>.GetLast()
        {
            throw new NotImplementedException();
        }

        IList<VehicleScaleDetailModel> IBaseDataAccess<VehicleScaleDetailModel>.GetAll()
        {
            throw new NotImplementedException();
        }

        public bool Add(VehicleScaleDetailModel item)
        {
            throw new NotImplementedException();
        }

        public bool Add(IList<VehicleScaleDetailModel> collection)
        {
            throw new NotImplementedException();
        }

        public bool Edit(VehicleScaleDetailModel item)
        {
            throw new NotImplementedException();
        }

        public bool Edit(IList<VehicleScaleDetailModel> collection)
        {
            throw new NotImplementedException();
        }

        public bool Delete(IList<VehicleScaleDetailModel> collection)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
